package com.sgb.kjwl.view.ui.adapter.shopping;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.sgb.kjwl.R;
import com.swgk.core.util.UiUtil;
import com.swgk.core.event.EventConstant;
import com.swgk.core.util.MyMediaUtils;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import cn.jzvd.JzvdStd;

public class NormalRecyclerAdapter1 extends RecyclerView.Adapter<NormalRecyclerAdapter1.NormalHolder> {

    private Context context;
    private List<String> urlList;
    private boolean mIsVideos; // 是否是视频集
    private boolean mIsMore; // 是否是轮播
    private int Type;

    public NormalRecyclerAdapter1(Context context, List<String> beans, boolean isVideos, boolean mIsMore, int type) {
        this.context = context;
        this.urlList = beans;
        mIsVideos = isVideos;
        this.mIsMore = mIsMore;
        this.Type = type;
    }

    @Override
    public NormalRecyclerAdapter1.NormalHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new NormalRecyclerAdapter1.NormalHolder(LayoutInflater.from(context).inflate(R.layout.adapter_data, parent, false));
    }

    @Override
    public void onBindViewHolder(NormalRecyclerAdapter1.NormalHolder holder, final int position) {
        if (urlList == null || urlList.isEmpty())
            return;
        String item = urlList.get(position % urlList.size());
        String key = "视频";
        holder.fl.setVisibility(item.contains(key) ? View.VISIBLE : View.GONE);
        holder.ll_bofang.setVisibility(!mIsVideos ? View.VISIBLE : View.GONE);
        holder.iv_img.setVisibility(!item.contains(key) ? View.VISIBLE : View.GONE);
        holder.jz_video.videos = mIsVideos;
        holder.jz_video.startButton.setVisibility(mIsVideos ? View.VISIBLE : View.GONE);
        holder.jz_video.setOnClick((state) ->
                {
                    if (mIsVideos) return;
                    if (Type == 0) {
                        UiUtil.sendMsg(EventConstant.EVENT_STATE, state);
                    } else {
                        UiUtil.sendMsg(EventConstant.EVENT_STATES, state);
                    }
                    holder.ll_bofang.setVisibility(state == 7 || state == 0 || state == 6 ? View.VISIBLE : View.GONE);
                }
        );
        holder.ll_bofang.setOnClickListener(v -> {
            holder.ll_bofang.setVisibility(View.GONE);
            holder.jz_video.startVideo();
        });

        if (item.contains(key)) {
            String[] str = item.split(key);
            holder.jz_video.setUp(str[0], "");
            if (TextUtils.isEmpty(str[1]) || ("null".equals(str[1]))) {
                holder.jz_video.posterImageView.setImageBitmap(MyMediaUtils.setVideoFirstImg(str[0], context));
            } else {
                Glide.with(context)
                        .load(str[1])
                        .into(holder.jz_video.posterImageView);
            }
        } else {
            Glide.with(context).load(item).into(holder.iv_img);
        }
    }

    @Override
    public int getItemCount() {
        return mIsMore && urlList.size() > 1 ? Integer.MAX_VALUE : urlList.size();
    }

    class NormalHolder extends RecyclerView.ViewHolder {
        ImageView iv_img, ll_bofang;
        JzvdStd jz_video;
        FrameLayout fl;

        public NormalHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            iv_img = itemView.findViewById(R.id.iv_img);
            ll_bofang = itemView.findViewById(R.id.ll_bofang);
            fl = itemView.findViewById(R.id.fl);
            jz_video = itemView.findViewById(R.id.jz_video);
            iv_img.setScaleType(ImageView.ScaleType.FIT_XY);
        }
    }

}
